/*
 * @Author: fishroom
 * @Date: 2022-10-12 16:40:03
 * @LastEditTime: 2023-01-03 14:06:02
 * @FilePath: \koa\app\controller\SysMenuController.ts
 */
import { Context } from "koa";
import SysMenuServer from "../server/SysMuenServer";
import response from "../util/response";
//数组转树
function arrTree(list: Array<any>) {
  let resultList: Array<any> = [];
  let sourceMap: any = {};
  list.forEach((item: any) => {
    sourceMap[item.menu_id] = item;
  });

  list.forEach((item) => {
    let parent = sourceMap[item.parent_id];
    if (parent) {
      item.parentName = parent.name;
      resultList.push(item);
    } else {
      item.parentName = "一级菜单";
      resultList.push(item);
    }
  });
  return resultList;
}

//数组转树
function arr2Tree(list: Array<any>) {
  let permissions: string = "";
  let resultList: Array<any> = [];
  let sourceMap: any = {};
  list.forEach((item: any) => {
    if (item == undefined || item.type == 2) {
      return;
    }
    sourceMap[item.menu_id] = item;
  });

  list.forEach((item, index) => {
    if (item == undefined) return;
    if (item.type == 2) {
      permissions += item.perms + ",";
      return;
    }
    let parent = sourceMap[item.parent_id];

    if (parent) {
      item.parentName = parent.name;

      (parent.list || (parent.list = [])).push(item);
    } else {
      resultList.push(item);
    }
  });
  return { menuList: resultList, permissions };
}
class SysMenuController {
  
  async getSysMenuNav(ctx: Context) {
    // console.log("token", ctx.$user);

    let data = await SysMenuServer.getSysMenuNav(ctx.$user.role_id);

    data = data.map((item) => {
      return item?.get();
    });
    // console.log("data",data);
    
    ctx.body = response.success(arr2Tree(data));
  }
  // 获取路由列表  // 没有一级
  async getSysMenuList(ctx: Context) {
    let data = await SysMenuServer.selectSysMenuNav({ menu_id: "!0" });
    ctx.body = response.success(arrTree(data));
  }
  // 根据id获取指定路由 不传参数有一级
  async getSysMenuSelect(ctx: Context) {
    let obj = ctx.query;
    let data = await SysMenuServer.SysMenuNavInfo(obj);
    ctx.body = response.success(data);
  }
  // 添加路由
  async saveSysMenu(ctx: Context) {
    let newNav = ctx.request.body;
    let { data, error } = await SysMenuServer.saveSysMenuNav(newNav);
    if (error) {
      return (ctx.body = response.error("添加失败", { error }));
    } else {
      return (ctx.body = response.success({ data }, "添加成功"));
    }
  }
  // 更新路由
  async updateSysMenu(ctx: Context) {
    let newNav = ctx.request.body;
    let { data, error } = await SysMenuServer.updateSysMenuNav(newNav);
    if (error) {
      return (ctx.body = response.error("修改失败", { error }));
    } else {
      return (ctx.body = response.success({ data }, "修改成功"));
    }
  }
  // 删除路由
  async deleteSysMenu(ctx: Context) {
    try {
      let menu_id = ctx.query["id"];
      let msg = await SysMenuServer.deleteSysMenu(menu_id as string);
      response.CxtSuccess(ctx, { msg });
    } catch (error) {
      response.CxtError(ctx, "失败", { error });
    }
  }
}
export default new SysMenuController();
